﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class User_Entity : EntityBase<USER>
    {
        public override RESP_Info Add(USER model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into [USER](USER_ID,USER_NAME,USER_NIKENAME,PASSWORD,EMAIL,TELEPHONE,ENABLE,USER_HEAD_IMG) values (@USER_ID,@USER_NAME,@USER_NIKENAME,@PASSWORD,@EMAIL,@TELEPHONE,@ENABLE,@USER_HEAD_IMG)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("USER_ID", model.USER_ID));
                    cmd.Parameters.Add(new SqlParameter("USER_NAME", model.USER_NAME));
                    cmd.Parameters.Add(new SqlParameter("USER_NIKENAME", model.USER_NIKENAME));
                    cmd.Parameters.Add(new SqlParameter("PASSWORD", model.PASSWORD));
                    cmd.Parameters.Add(new SqlParameter("EMAIL", model.EMAIL));
                    cmd.Parameters.Add(new SqlParameter("TELEPHONE", model.TELEPHONE));
                    cmd.Parameters.Add(new SqlParameter("ENABLE", model.ENABLE));
                    cmd.Parameters.Add(new SqlParameter("USER_HEAD_IMG", model.USER_HEAD_IMG));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加用户成功</p></h5>";
                    info.Url = "UserList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加用户失败</p></h5>";
                    info.Url = "UserAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加用户失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(USER model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update [USER] set USER_NAME=@USER_NAME,USER_NIKENAME=@USER_NIKENAME,PASSWORD=@PASSWORD,EMAIL=@EMAIL,TELEPHONE=@TELEPHONE,ENABLE=@ENABLE,USER_HEAD_IMG=@USER_HEAD_IMG where USER_ID=@USER_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("USER_ID", model.USER_ID));
                    cmd.Parameters.Add(new SqlParameter("USER_NAME", model.USER_NAME));
                    cmd.Parameters.Add(new SqlParameter("USER_NIKENAME", model.USER_NIKENAME));
                    cmd.Parameters.Add(new SqlParameter("PASSWORD", model.PASSWORD));
                    cmd.Parameters.Add(new SqlParameter("EMAIL", model.EMAIL));
                    cmd.Parameters.Add(new SqlParameter("TELEPHONE", model.TELEPHONE));
                    cmd.Parameters.Add(new SqlParameter("ENABLE", model.ENABLE));
                    cmd.Parameters.Add(new SqlParameter("USER_HEAD_IMG", model.USER_HEAD_IMG));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑用户成功</p></h5>";
                    info.Url = "UserList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑用户失败</p></h5>";
                    info.Url = "UserAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑用户失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(USER user)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from [USER] where USER_ID=@USER_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("USER_ID", user.USER_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除用户成功</p></h5>";
                    info.Url = "UserList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除用户失败</p></h5>";
                    info.Url = "UserList.aspx";

                    Tom.Net.Log.LogManager.Error("删除用户失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override USER GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select USER_ID,USER_NAME,USER_NIKENAME,PASSWORD,EMAIL,TELEPHONE,ENABLE,USER_HEAD_IMG from [USER] where USER_ID=@USER_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("USER_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        USER user = new USER();
                        if (!reader.IsDBNull(0)) user.USER_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) user.USER_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) user.USER_NIKENAME = reader.GetString(2);
                        if (!reader.IsDBNull(3)) user.PASSWORD = reader.GetString(3);
                        if (!reader.IsDBNull(4)) user.EMAIL = reader.GetString(4);
                        if (!reader.IsDBNull(5)) user.TELEPHONE = reader.GetString(5);
                        if (!reader.IsDBNull(6)) user.ENABLE = reader.GetInt16(6);
                        if (!reader.IsDBNull(7)) user.USER_HEAD_IMG = reader.GetString(7);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return user;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取用户数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override USER GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        USER user = new USER();
                        if (!reader.IsDBNull(0)) user.USER_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) user.USER_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) user.USER_NIKENAME = reader.GetString(2);
                        if (!reader.IsDBNull(3)) user.PASSWORD = reader.GetString(3);
                        if (!reader.IsDBNull(4)) user.EMAIL = reader.GetString(4);
                        if (!reader.IsDBNull(5)) user.TELEPHONE = reader.GetString(5);
                        if (!reader.IsDBNull(6)) user.ENABLE = reader.GetInt16(6);
                        if (!reader.IsDBNull(7)) user.USER_HEAD_IMG = reader.GetString(7);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return user;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取用户数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<USER> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<USER> users = new List<USER>();
                    while (reader.Read())
                    {
                        USER user = new USER();
                        if (!reader.IsDBNull(0)) user.USER_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) user.USER_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) user.USER_NIKENAME = reader.GetString(2);
                        if (!reader.IsDBNull(3)) user.PASSWORD = reader.GetString(3);
                        if (!reader.IsDBNull(4)) user.EMAIL = reader.GetString(4);
                        if (!reader.IsDBNull(5)) user.TELEPHONE = reader.GetString(5);
                        if (!reader.IsDBNull(6)) user.ENABLE = reader.GetInt16(6);
                        if (!reader.IsDBNull(7)) user.USER_HEAD_IMG = reader.GetString(7);

                        users.Add(user);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return users;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取用户数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}
